package com.zbcz.erp.activity

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.launcher.ARouter
import com.zbcz.erp.adapter.CommonEditFileAdapter
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.bean.FileItem
import com.zbcz.erp.databinding.ActivityApprovalAdviceBinding
import com.zbcz.erp.fragment.CommonListFragment.Companion.DONE_JOB_BEAN
import com.zbcz.erp.util.DensityUtils
import com.zbcz.erp.util.ToastUtils
import com.zbcz.erp.util.onSingleClick
import com.zbcz.erp.viewmodel.ApprovalAdviceViewModel
import java.io.File

@Deprecated("")
class ApprovalAdviceActivity : BaseActivity() {

    private lateinit var mBinding: ActivityApprovalAdviceBinding
    private val mViewModel: ApprovalAdviceViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = ActivityApprovalAdviceBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        ARouter.getInstance().inject(this)
        init()
        super.onCreate(savedInstanceState)
    }

    var bean: DoneJobBean? = null

    fun init() {
        bean = intent?.getParcelableExtra(DONE_JOB_BEAN) as DoneJobBean?
        mBinding.clParent.setPadding(
            0,
            DensityUtils.dp2px(this, 48f),
            0,
            0
        )
        mBinding.clTitle.tvTitle.text = bean?.name
        mBinding.clTitle.ivCommonBack.onSingleClick {
            finish()
        }
        mFileAdapter = CommonEditFileAdapter(this)
        mBinding.rcFileList.adapter = mFileAdapter
        mFileAdapter?.apply {
            submitList(listOf(FileItem(null, CommonEditFileAdapter.ADD_FILE_TYPE)))
        }
        mBinding.tvConfirm.onSingleClick {
            val mTargetFile = mFileAdapter?.getTargetFile(this)
            onConfirmClick(null, true)
        }
        mBinding.tvCancel.onSingleClick {
            val mTargetFile = mFileAdapter?.getTargetFile(this)
            onConfirmClick(null, false)
        }

        mViewModel.mActPassLiveData.observe(this) { result ->
            if (result.first) {
                ToastUtils.show(result.second ?: "")
                finish()
            } else {
                ToastUtils.show(result.second ?: "")
            }
        }
        mViewModel.mActBackLiveData.observe(this) { result ->
            if (result.first) {
                ToastUtils.show(result.second ?: "")
                finish()
            } else {
                ToastUtils.show(result.second ?: "")
            }
        }

        mBinding.edContentAdvice.addTextChangedListener {
            val textSize = it.toString().length
            mBinding.tvCount.text = "$textSize/100"
        }
    }


    private var mFileAdapter: CommonEditFileAdapter? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ErpWorkProcessActivity.REQUEST_CODE_OPEN_DOCUMENT && resultCode == RESULT_OK) {
            if (data != null) {
                //content://com.android.providers.media.documents/document/document%3A86986
                val fileUri = data.data
                Log.d(ErpWorkProcessActivity.TAG, fileUri.toString())
                mFileAdapter?.addData(fileUri)
            }
        }
    }

    private fun onConfirmClick(mTargetFile: File?, isPass: Boolean) {
        if (bean != null) {
            mViewModel.actPass(
                isPass,
                bean?.id ?: "",
                bean?.procInstId ?: "",
                mBinding.edContentAdvice.text.toString()
            )
        }
    }
}